import axios from "axios";

import UseUserStore from "@/stores/modules/user";

import 'element-plus/theme-chalk/el-message.css'
import { ElMessage } from 'element-plus'
import router from "@/router";

const request = axios.create({
    baseURL:'http://pcapi-xiaotuxian-front-devtest.itheima.net',
    timeout:5000
})
//请求拦截器
request.interceptors.request.use((config)=>{
    let UserStore = UseUserStore()
    if(UserStore.token) {
        config.headers.Authorization = `Bearer ${UserStore.token}` 
    }
   
    return config
},e=>{
    return Promise.reject(e)
})
//响应拦截器
request.interceptors.response.use(res=>{
    return res.data
},e=>{
    ElMessage({
        type:'warning',
        message:e.response.data.message
    })
    if(e.response.status === 401) {
        UserStore.UserLogout()
        router.push('/login')
    }
    return Promise.reject(e)
})

export default request